//
//  BGRenZhengCenterViewController.m
//  bigMall
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGRenZhengCenterViewController.h"
#import "BGRenZhengoneCell.h"
#import "BGRenZhengtwoCell.h"
#import "BGDateResult.h"
#import "BGAreaResultModel.h"
#import "BGAreaDataModel.h"
#import "AFNetworking.h"
#import "BGParam.h"
#import "MJExtension.h"
#import "BGHttpTool.h"
#import "AFHttpSessionManager.h"
#import "BGFileUpLoadModel.h"
#import "BGfileupload.h"
#import "BGSubmitAuthInfoModel.h"
#import "BGBasicUrl.h"
#import "BGUserInfoMode.h"
#import "UIImageView+WebCache.h"
@interface BGRenZhengCenterViewController () <UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate,UITextViewDelegate> {
    
    UITableView *_tableView;
    NSArray *_dataArr;
    UIButton *_btn;
    UIImage *_image;
    NSMutableArray *_arr;
    UIView *v;
    NSString *place;
    NSMutableArray *oneArr;
    NSMutableArray *twoArr;
    NSMutableArray *threeArr;
    NSMutableArray *oneTitleArr;
    NSMutableArray *twoTitleArr;
    NSMutableArray *threeTitleArr;
    NSString *str1;
    NSString *str2;
    NSString *str3;
    UITapGestureRecognizer *tap1;
    UITapGestureRecognizer *tap2;
    UITapGestureRecognizer *tap3;
    
    NSMutableArray *oneAreaId;//地址的Id
    NSMutableArray *twoAreaId;
    NSMutableArray *threeAreaId;
    
    NSString *areaIdStr1;
    NSString *areaIdStr2;
    NSString *areaIdStr3;
    
    //提交的参数
    NSString *name;
    NSString *area;
    NSString *address;
    NSString *phone;
    NSString *introduce;
    NSString *images;
    
    NSString *nameStr;
    NSString *idStr;
    NSString *urlStr;
    
    NSMutableArray *imageUrlArr;//保存上传图片的路径
    NSMutableArray *nameArr;
    
    NSString *yingyezhizhao;
    NSString *zhengmian;
    NSString *fanmian;
    
}
@property (nonatomic,strong) UIPickerView *picker;
@property (nonatomic,copy) NSString *com_name;//企业的名称
@property (nonatomic,copy) NSString *areaName;//企业所在地
@property (nonatomic,copy) NSString *com_add;//企业的地址
@property (nonatomic,copy) NSString *com_phone;//企业的电话
@property (nonatomic,copy) NSString *com_cont;//企业的介绍
@end

@implementation BGRenZhengCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"企业认证";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadInter];
    [self createUI];
    [self initDat];
    [self loadPlist];
    //    [self loadInter];
    [self createPicker];
    
}

- (void)loadInter {
    
    [BGDateResult getUserInfoSuccess:^(NSString *status, NSString *msg, NSDictionary *res) {
        
        if ([status intValue] == 1) {
            
//            BGUserInfoMode *model = (BGUserInfoMode *)res;
            
            areaIdStr1 = res[@"province"];
            areaIdStr2 = res[@"city"];
            areaIdStr3 = res[@"county"];
            
            self.com_name = res[@"com_name"];
            self.com_add = res[@"com_add"];
            self.com_cont = res[@"com_cont"];
            self.com_phone = res[@"com_phone"];
            self.areaName = res[@"com_area_name"];
            NSLog(@"model.com_area_name === %@",res[@"com_area_name"]);
            NSLog(@"model.check_status === %@",res[@"check_status"]);
            self.check_status = res[@"check_status"];
            
            yingyezhizhao = res[@"img_src"][0];
            zhengmian = res[@"img_src"][1];
            fanmian = res[@"img_src"][2];
            
            if ([res[@"check_status"] isEqualToString:@"0"] || [res[@"check_status"] isEqualToString:@"-1"]) {
                _btn.userInteractionEnabled = YES;
                [_btn setTitle:@"提交信息" forState:UIControlStateNormal];
                _btn.backgroundColor = [UIColor redColor];
            } else if ([res[@"check_status"] isEqualToString:@"1"]) {
                [_btn setTitle:@"审核等待" forState:UIControlStateNormal];
                _btn.userInteractionEnabled = NO;
                _btn.backgroundColor = lightGray_;
            } else if ([res[@"check_status"] isEqualToString:@"2"]) {
                [_btn setTitle:@"审核通过" forState:UIControlStateNormal];
                _btn.userInteractionEnabled = NO;
                _btn.backgroundColor = lightGray_;
            } else {
                [self showMessage:@"审核不通过，请重新提交!"];
                [_btn setTitle:@"提交信息" forState:UIControlStateNormal];
                _btn.userInteractionEnabled = YES;
                _btn.backgroundColor = [UIColor redColor];
            }
            
            
            
        }else {
            [self showMessage:msg];
        }
        
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)loadPlist {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@".plist"];
    
    NSArray *mainArr = [NSArray arrayWithContentsOfFile:path];
    
    oneArr = mainArr[0];
    
    oneAreaId = [NSMutableArray new];
    
    for (NSDictionary *dic in oneArr) {
        
        [oneTitleArr addObject:dic[@"name"]];
        [oneAreaId addObject:dic[@"id"]];
        
    }
    
}

- (void)createPicker {
    
    self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bottom - BGHight / 3 - BGHight / 10 + 10, BGWidth, BGHight / 3)];
    
    self.picker.delegate = self;
    self.picker.dataSource = self;
    self.picker.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.picker];
    [self.picker showsSelectionIndicator];
    
    [self.picker reloadAllComponents];
    self.picker.hidden = YES;
    
    //创建确定的按钮
    v = [[UIView alloc] initWithFrame:CGRectMake(0, self.picker.y - 16, BGWidth, 30)];
    v.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:v];
    
    UIView *a = [[UIView alloc] initWithFrame:CGRectMake(0, 29, BGWidth, 1)];
    a.backgroundColor = [UIColor lightGrayColor];
    [v addSubview:a];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(v.width - BGWidth / 4.3, 0, BGWidth / 4.3, 30)];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn .titleLabel.textAlignment = NSTextAlignmentRight;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    v.hidden = YES;
    [v addSubview:btn];
    
}

- (void)xianshi {
    
    self.picker.hidden = NO;
    v.hidden = NO;
    
}

- (void)btnClick {
    
    self.picker.hidden = YES;
    v.hidden = YES;
    _picker.hidden = YES;
    
}
#pragma mark pickerDatasourece
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 3;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        
        //省
        return oneArr.count;
        
    }else if (component == 1) {
        
        //市
        NSInteger selectedProvinceIndex = [self.picker selectedRowInComponent:0];
        
        NSDictionary *dic = oneArr[selectedProvinceIndex];
        
        twoArr = dic[@"child"];
        
        NSMutableArray *title = [NSMutableArray array];
        
        twoAreaId = [NSMutableArray array];
        
        for (NSDictionary *dic in twoArr) {
            
            [title addObject:dic[@"name"]];
            
            [twoAreaId addObject:dic[@"id"]];
            
        }
        
        twoTitleArr = [NSMutableArray arrayWithArray:title];
        
        return twoArr.count;
        
    }else {
        
        //县
        NSInteger selectedProvinceIndex = [self.picker selectedRowInComponent:1];
        
        NSDictionary *dic = twoArr[selectedProvinceIndex];
        
        threeArr = dic[@"child"];
        
        NSMutableArray *title = [NSMutableArray array];
        
        threeAreaId = [NSMutableArray array];
        
        
        for (NSDictionary *dic in threeArr) {
            
            [title addObject:dic[@"name"]];
            
            [threeAreaId addObject:dic[@"id"]];
            
        }
        
        threeTitleArr = [NSMutableArray arrayWithArray:title];
        
        str1 = oneTitleArr[0];
        areaIdStr1 = oneAreaId[0];
        
        str2 = twoTitleArr[0];
        areaIdStr2 = twoAreaId[0];
        
        str3 = threeTitleArr[0];
        areaIdStr3 = threeAreaId[0];
        
        return threeArr.count;
        
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        
        NSString *str = oneTitleArr[row];
        
        return str;
        
    }else if (component == 1) {
        
        NSString *str = twoTitleArr[row];
        
        return str;
        
    }else {
        
        NSString *str = threeTitleArr[row];
        
        return str;
        
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        
        [self.picker reloadComponent:1];
        [self.picker reloadComponent:2];
        str1 = oneTitleArr[row];
        areaIdStr1 = oneAreaId[row];
        
    }else if (component == 1) {
        
        [self.picker reloadComponent:2];
        str2 = twoTitleArr[row];
        areaIdStr2 = twoAreaId[row];
        
    }else {
        
        str3 = threeTitleArr[row];
        areaIdStr3 = threeAreaId[row];
        
    }
    
    place = [NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3,nil];
    self.areaName = place;
    
    //    [[NSUserDefaults standardUserDefaults] setObject:place forKey:@"qiyesuozaidi"];
    //    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [_tableView reloadData];
    
}


- (void)initDat {
    
    nameArr = [NSMutableArray array];
    imageUrlArr = [NSMutableArray array];
    twoArr = [NSMutableArray array];
    threeArr = [NSMutableArray array];
    oneTitleArr = [NSMutableArray array];
    twoTitleArr = [NSMutableArray array];
    threeTitleArr = [NSMutableArray array];
    _dataArr = @[@"企业名称 :",@"企业所在地 :",@"企业地址 :",@"企业电话 :"];
    _arr = [NSMutableArray array];
    
}

- (void)createUI {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight - 70) style:UITableViewStyleGrouped];
    
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    //login
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(21, BGHight - 60, self.view.bounds.size.width - 42, 50)];
    
    _btn.clipsToBounds = YES;
    
    _btn.layer.cornerRadius = 10;
    
    [_btn setTitle:@"提交信息" forState:UIControlStateNormal];
    
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [_btn addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_btn];
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)tijiao {
    
    if (self.com_name == nil || self.com_add == nil || areaIdStr1 == nil || areaIdStr2 == nil || areaIdStr3 == nil || self.com_phone == nil || [self.com_phone isEqualToString:@""] || [self.com_name isEqual:@""]|| [self.com_add isEqual:@""] || [areaIdStr1 isEqual:@""] || [areaIdStr2 isEqual:@""] || [areaIdStr3 isEqual:@""] || imageUrlArr.count != 3 || nameArr.count != 3) {
        
        [self showMessage:@"信息不全,请完善"];
        
        return;
        
    }
    NSLog(@"%@",@{@"province":areaIdStr1,
                  @"city":areaIdStr2,
                  @"county":areaIdStr3,
                  @"com_add":self.com_add,
                  @"com_cont":self.com_cont,
                  @"com_name":self.com_name,
                  @"com_phone":self.com_phone,
                  @"imgname":[NSString stringWithFormat:@"%@,%@,%@",nameArr[0],nameArr[1],nameArr[2]],
                  @"imgsrc":[NSString stringWithFormat:@"%@,%@,%@",imageUrlArr[0],imageUrlArr[1],imageUrlArr[2]]});
    [BGDateResult submitAuthInfoWithParamDic:@{@"province":areaIdStr1,
                                               @"city":areaIdStr2,
                                               @"county":areaIdStr3,
                                               @"com_add":self.com_add,
                                               @"com_cont":self.com_cont,
                                               @"com_name":self.com_name,
                                               @"com_phone":self.com_phone,
                                               @"imgname":[NSString stringWithFormat:@"%@,%@,%@",nameArr[0],nameArr[1],nameArr[2]],
                                               @"imgsrc":[NSString stringWithFormat:@"%@,%@,%@",imageUrlArr[0],imageUrlArr[1],imageUrlArr[2]]}
                                     success:^(NSObject *result) {
                                         
                                         BGSubmitAuthInfoModel *model = (BGSubmitAuthInfoModel *)result;
                                         
                                         if ([model.status isEqualToString:@"1"]) {
                                             
                                             [self showMessage:model.msg];
                                             
                                         } else {
                                             
                                             [self showMessage:model.msg];
                                             
                                         }
                                         
                                     } failure:^(NSError *error) {
                                         
                                         
                                         
                                     }];
    
    //    for (BGJieTiJiaGeCell *cel in _tableView.visibleCells) {
    //
    //        [arr addObject:cel.shuliang.text];
    //
    //    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row <4) {
            
            [tableView registerNib:[UINib nibWithNibName:@"BGRenZhengoneCell" bundle:nil] forCellReuseIdentifier:@"celId"];
            
            BGRenZhengoneCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
            cel.title.text = _dataArr[indexPath.row];
            cel.mesage.borderStyle = UITextBorderStyleNone;
            cel.mesage.delegate = self;
            cel.mesage.tag = indexPath.row + 10;
            cel.selectionStyle = UITableViewCellSelectionStyleNone;
            [cel.mesage addTarget:self action:@selector(textFiledValuechange:) forControlEvents:UIControlEventEditingChanged];
            if (indexPath.row == 1) {
                
                cel.mesage.placeholder = @"请选择";
                cel.mesage.text = self.areaName;
                //                cel.mesage.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"qiyesuozaidi"];
                cel.mesage.userInteractionEnabled = NO;
                cel.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cel.selectionStyle = UITableViewCellSelectionStyleDefault;
                
            }
            
            if (indexPath.row == 3) {
                
                cel.mesage.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                
            }
            
            if (indexPath.row == 0) {
                
                cel.mesage.text = self.com_name;
                
            } else if (indexPath.row == 1) {
                
                NSLog(@"self.areaName === %@",self.areaName);
                
                cel.mesage.text = self.areaName;
                
            } else if (indexPath.row == 2) {
                
                cel.mesage.text = self.com_add;
                
            } else if (indexPath.row == 3) {
                
                cel.mesage.text = self.com_phone;
                
            }
            NSLog(@"self.check_status === %@",self.check_status);
            if ([self.check_status isEqualToString:@"0"] || [self.check_status isEqualToString:@"-1"]) {
                cel.userInteractionEnabled = YES;
            }else if ([self.check_status isEqualToString:@"3"]) {
                cel.userInteractionEnabled = YES;
            }
            else {
                cel.userInteractionEnabled = NO;
            }
            
            return cel;
            
        }else {
            
            [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"celId"];
            
            UITableViewCell *cel = [[UITableViewCell alloc] init];
            
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(17, 19, 89, 21)];
            lab.text = @"企业介绍:";
            lab.font = [UIFont systemFontOfSize:16];
            [cel addSubview:lab];
            
            UITextView *t = [[UITextView alloc] initWithFrame:CGRectMake(lab.right, 0, BGWidth - 34 - lab.right, 100)];
            
            t.delegate = self;
            t.textColor = [UIColor lightGrayColor];
            t.font = [UIFont systemFontOfSize:16];
            NSLog(@"self.check_status === %@",self.check_status);
            if ([self.check_status isEqualToString:@"0"] || [self.check_status isEqualToString:@"-1"]) {
                cel.userInteractionEnabled = YES;
                t.text = self.com_cont;
            }else if ([self.check_status isEqualToString:@"3"]) {
                cel.userInteractionEnabled = YES;
                t.text = self.com_cont;
            }
            else {
                t.text = self.com_cont;
                self.com_cont = t.text;
                cel.userInteractionEnabled = NO;
            }
            //            t.text = @"企业介绍：成为世界一流的煤炭科技产业集团，是企业发张的内在要求。“世界一流”，是指接团公司拥有世界一流的核心技术、世界一流的管理水平";
            [cel addSubview:t];
            return cel;
            
        }
        
    }else {
        
        [tableView registerNib:[UINib nibWithNibName:@"BGRenZhengtwoCell" bundle:nil] forCellReuseIdentifier:@"celId"];
        BGRenZhengtwoCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
        
        tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1:)];
        tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click2:)];
        tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click3:)];
        [cel.yingyezhizhao addGestureRecognizer:tap1];
        [cel.zhengmian addGestureRecognizer:tap2];
        [cel.fanmian addGestureRecognizer:tap3];
        
        if ([self.check_status isEqualToString:@"0"] || [self.check_status isEqualToString:@"-1"] ) {
            cel.userInteractionEnabled = YES;
            if (_arr.count == 1) {
                cel.yingyezhizhao.image = [UIImage imageWithData:_arr[0]];
            }
            
            if (_arr.count == 2) {
                cel.zhengmian.image = [UIImage imageWithData:_arr[1]];
            }
            
            if (_arr.count == 3) {
                cel.fanmian.image  = [UIImage imageWithData:_arr[2]];
            }
            
        }else if ([self.check_status isEqualToString:@"3"]) {
            cel.userInteractionEnabled = YES;
            if (_arr.count == 1) {
                cel.yingyezhizhao.image = [UIImage imageWithData:_arr[0]];
            }
            
            if (_arr.count == 2) {
                cel.zhengmian.image = [UIImage imageWithData:_arr[1]];
            }
            
            if (_arr.count == 3) {
                cel.fanmian.image  = [UIImage imageWithData:_arr[2]];
            }
        }
        else {
            cel.userInteractionEnabled = NO;
            
            [cel.yingyezhizhao sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[BGBasicUrl getBasicUrl],yingyezhizhao]]];
            [cel.zhengmian sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[BGBasicUrl getBasicUrl],zhengmian]]];
            [cel.fanmian sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[BGBasicUrl getBasicUrl],fanmian]]];
            
        }
        
        
        return cel;
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 1) {
            
            [self xianshi];
            
        }
        
    }
    
}

#pragma mark - dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 5;
        
    }else {
        
        return 1;
        
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 10;
        
    }else {
        
        return 10;
        
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row <4) {
            
            return 61;
            
        }else {
            
            return 100;
            
        }
        
    }else {
        
        return 225;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 1;
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
        
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
        
    }
    
}

- (void)click1:(UITapGestureRecognizer *)tap {
    
    if (_arr.count == 1) {
        return;
    }
    
    UIAlertController *action = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //拍照
        [self readImageFromCamera];
        
    }];
    [action addAction:a1];
    
    UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //从相册中读取
        [self readImageFromAlbum];
        
    }];
    [action addAction:a2];
    
    UIAlertAction *a3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [action addAction:a3];
    
    [self presentViewController:action animated:YES completion:^{
        
    }];
    //    tap.enabled = NO;
    
}
- (void)click2:(UITapGestureRecognizer *)tap {
    
    if (_arr.count == 2) {
        return;
    }
    
    UIAlertController *action = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //拍照
        [self readImageFromCamera];
        
    }];
    [action addAction:a1];
    
    UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //从相册中读取
        [self readImageFromAlbum];
        
    }];
    [action addAction:a2];
    
    UIAlertAction *a3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [action addAction:a3];
    
    [self presentViewController:action animated:YES completion:^{
        
    }];
    //    tap.enabled = NO;
    
}
- (void)click3:(UITapGestureRecognizer *)tap {
    
    if (_arr.count == 3) {
        return;
    }
    
    UIAlertController *action = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //拍照
        [self readImageFromCamera];
        
    }];
    [action addAction:a1];
    
    UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //从相册中读取
        [self readImageFromAlbum];
        
    }];
    [action addAction:a2];
    
    UIAlertAction *a3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [action addAction:a3];
    
    [self presentViewController:action animated:YES completion:^{
        
    }];
    
    
}
//从相册中读取
- (void)readImageFromAlbum {
    //创建对象
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    //（选择类型）表示仅仅从相册中选取照片
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //指定代理，因此我们要实现UIImagePickerControllerDelegate,
    //UINavigationControllerDelegate协议
    imagePicker.delegate = self;
    //设置在相册选完照片后，是否跳到编辑模式进行图片剪裁。(允许用户编辑)
    imagePicker.allowsEditing = YES;
    //显示相册
    [self presentViewController:imagePicker animated:YES completion:nil];
    
}
//图片完成之后处理

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
    //image 就是修改后的照片
    //将图片添加到对应的视图上
    _image = image;
    NSData *fData = UIImageJPEGRepresentation(_image, 0.1);
    [_arr addObject:fData];
    [_tableView reloadData];
    
    //结束操作
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //图片的上传
    NSString *path =  [NSString stringWithFormat:@"%@/Api_1/Index/upload",[BGBasicUrl getBasicUrl]];
    //参数列表（字典类型啊）
    NSDictionary *paramters = @{@"obj_id":@"123",@"type":@"mall"};
    [BGfileupload getFileUpLoadPath:path andParamters:paramters andData:fData success:^(NSObject *object) {
        
        BGFileUpLoadModel *model = (BGFileUpLoadModel *)object;
        if ([model.status isEqualToString:@"1"]) {
            
            NSDictionary *res = [NSDictionary dictionaryWithDictionary:model.res];
            NSArray *arr = [res objectForKey:@"arr"];
            NSDictionary *dic = arr[0];
            nameStr = [dic objectForKey:@"name"];
            urlStr = [dic objectForKey:@"url"];
            idStr = [dic objectForKey:@"id"];
            
            [imageUrlArr addObject:urlStr];
            [nameArr addObject:nameStr];
            
        }else {
            
            [self showMessage:model.msg];
            
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)readImageFromCamera {
    
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES; //允许用户编辑
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    } else {
        //弹出窗口响应点击事件
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"未检测到摄像头" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])  {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    
    self.com_cont = textView.text;
    
}
- (void)textFiledValuechange:(UITextField *)textFiled {
    
    switch (textFiled.tag) {
        case 10:
            self.com_name = textFiled.text;
            break;
        case 11:
            self.areaName = textFiled.text;
            break;
        case 12:
            self.com_add = textFiled.text;
            break;
        case 13:
            self.com_phone = textFiled.text;
            break;
        default:
            break;
    }
    
}
@end
